package de.herzog.mtv.bb;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

public class ExcelRow {

	private Map<String, Object> columns = new HashMap<>();
	private int rowIndex;

	public ExcelRow(HSSFRow row, Map<Integer, String> headers, int rowIndex) {
		for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
			setColumn(headers.get(cellNum), row.getCell(cellNum));
		}
		this.rowIndex = rowIndex;
		setColumn("Rowindex", rowIndex);
	}

	public ExcelRow(int rowIndex) {
		this.rowIndex = rowIndex;
		setColumn("Rowindex", rowIndex);
	}

	public Object getColumn(String columnName) {
		return columns.get(columnName);
	}

	public Object setColumn(String columnName, Object columnValue) {
		Object oldValue = columns.get(columnName);
		columns.put(columnName, columnValue);
		return oldValue;
	}

	public Object setColumn(String columnName, HSSFCell cell) {
		if (cell != null) {
			switch (cell.getCellType()) {
			case HSSFCell.CELL_TYPE_STRING:
				return setColumn(columnName, cell.getStringCellValue());
			case HSSFCell.CELL_TYPE_BOOLEAN:
				return setColumn(columnName, cell.getBooleanCellValue());
			case HSSFCell.CELL_TYPE_NUMERIC:
				return setColumn(columnName, cell.getNumericCellValue());
			case HSSFCell.CELL_TYPE_BLANK:
				return setColumn(columnName, null);
			default:
				return null;
			}
		} else {
			return null;
		}
	}

	public boolean hasColumnValue(String columnName, Object columnValue) {
		Object columnValue2 = getColumn(columnName);
		if (columnValue == null)
			return columnValue2 == null;
		else
			return columnValue.equals(columnValue2);
	}

	@Override
	public String toString() {
		return columns.toString();
	}

	public int getRowIndex() {
		return rowIndex;
	}
}
